const queue = require('../services/clusterService.js')
const registerCluster = async ctx => {
  const data = ctx.request.body
  console.log('registerCluster', data)
  try {
    const result = await new Promise((resolve, reject) => {
      queue.push({ action: 'register', payload: data }, (err, result) => {
        if (err) return reject(err)
        resolve(result)
      })
    })
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '注册集群节点成功！',
      data: result
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}
const updateCluster = async ctx => {
  const clusterUpdate = ctx.request.body

  try {
    const result = await new Promise((resolve, reject) => {
      queue.push(
        { action: 'heartbeat', payload: clusterUpdate },
        (err, result) => {
          if (err) return reject(err)
          resolve(result)
        }
      )
    })

    ctx.status = 200
    ctx.body = {
      success: true,
      message: '集群节点心跳检测成功！',
      cluster: result
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}

const offlineCluster = async ctx => {
  const clusterData = ctx.request.body

  try {
    const result = await new Promise((resolve, reject) => {
      queue.push({ action: 'offline', payload: clusterData }, (err, result) => {
        if (err) return reject(err)
        resolve(result)
      })
    })

    ctx.status = 200
    ctx.body = { status: true, message: '下线集群节点成功！', data: result }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}
const getCluster = async ctx => {
  const queryData = ctx.request.body
  try {
    const result = await new Promise((resolve, reject) => {
      queue.push({ action: 'getData', payload: queryData }, (err, result) => {
        if (err) return reject(err)
        resolve(result)
      })
    })
    ctx.status = 200
    ctx.body = {
      status: true,
      message: '获取集群节点成功！',
      data: result.records,
      page: {
        total: result.total,
        pageNo: queryData.pageNo,
        pages: Math.ceil(result.total / queryData.pageSize)
      }
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}

const deleteCluster = async ctx => {
  const deleteData = ctx.request.body
  try {
    await new Promise((resolve, reject) => {
      queue.push(
        { action: 'deleteCluster', payload: deleteData },
        (err, result) => {
          if (err) return reject(err)
          resolve(result)
        }
      )
    })
    ctx.status = 200
    ctx.body = { status: true, message: '删除集群节点成功！' }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}
const startCluster = async ctx => {
  const clusterData = ctx.request.body

  try {
    await new Promise((resolve, reject) => {
      queue.push(
        { action: 'startCluster', payload: clusterData },
        (err, result) => {
          if (err) return reject(err)
          resolve(result)
        }
      )
    })

    ctx.status = 200
    ctx.body = { status: true, message: '上线集群节点成功！' }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}
const getprocessById = async ctx => {
  const processData = ctx.query
  try {
    const result = await new Promise((resolve, reject) => {
      queue.push(
        { action: 'getprocessById', payload: processData },
        (err, result) => {
          if (err) return reject(err)
          resolve(result)
        }
      )
    })

    ctx.status = 200
    ctx.body = { status: true, message: '获取集群节点进程成功！', data: result }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}

const reloadProcess = async ctx => {
  const processData = ctx.request.body
  try {
    const result = await new Promise((resolve, reject) => {
      queue.push(
        { action: 'reloadProcess', payload: processData },
        (err, result) => {
          if (err) return reject(err)
          resolve(result)
        }
      )
    })

    ctx.status = 200
    ctx.body = {
      status: true,
      message: `重启集群节点进程：${processData.processName}成功！`,
      data: result
    }
  } catch (err) {
    throw new HttpError(err.message, 400)
  }
}

module.exports = {
  registerCluster,
  getCluster,
  updateCluster,
  offlineCluster,
  deleteCluster,
  startCluster,
  getprocessById,
  reloadProcess
}
